import axios from 'axios'
export const SendPlatformApi = (method, path, param = {}, data, urlParamList = [], host, port, token) => {
  let headerInfo = {}
  let defaultParam = JSON.parse('{}')
  let baseURL = `http://192.168.13.103:${port}/openatc`
  if (host !== '') {
    baseURL = `http://${host}:${port}/openatc`
  }
  if (token) {
    headerInfo.Authorization = token
  }
  let apiUrl = path
  for (let item of urlParamList) {
    apiUrl += '/'
    apiUrl += item
  }
  const config = {
    baseURL: baseURL,
    url: apiUrl,
    method: method,
    headers: headerInfo,
    timeout: 1000 * 60,
    data: data,
    params: _assingJson(param, defaultParam)
  }
  const service = axios.create()
  return service({...config})
}

export function _assingJson (json1, json2) {
  if (JSON.stringify(json1) === '{}') return json2
  if (JSON.stringify(json2) === '{}') return json1
  return JSON.parse((JSON.stringify(json1) + JSON.stringify(json2)).replace(/}{/, ','))
}
